//Examples/reed_sol_rs_02.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerasure.h"
#include "reed_sol.h"

#define talloc(type, num) (type *) malloc(sizeof(type)*(num))

void usage(char *s)
{
	fprintf(stderr, "usage: reed_sol_rs_02 k m w - Vandermonde matrices in GF(2^w).\n");
	fprintf(stderr, "       \n");
	fprintf(stderr, "       k+m must be <= 2^w.  This simply prints out the \n");
	fprintf(stderr, "       Vandermonde matrix in GF(2^w), and then the distribution\n");
	fprintf(stderr, "       matrix that is constructed from it.  See [Plank-Ding-05] for\n");
	fprintf(stderr, "       information on how this construction proceeds\n");
	fprintf(stderr, "       \n");
	fprintf(stderr, "This demonstrates: RS_Nonsystematic_Matrix()\n");
	fprintf(stderr, "                   RS_Extended_Matrix()\n");
	fprintf(stderr, "                   RS_Classic_Matrix()\n");
	fprintf(stderr, "                   JER_Matrix::Print()\n");
	if (s != NULL) fprintf(stderr, "%s\n", s);
	exit(1);
}

int main(int argc, char **argv)
{
	uint64_t l;
	int k, w, i, j, m;
	JER_Matrix *jm;

	if (argc != 4) usage(NULL);
	if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage((char *)"Bad k");
	if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage((char *)"Bad m");
	if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage((char *)"Bad w");
	if (w <= 30 && k + m > (1 << w)) usage((char *)"k + m is too big");

	jm = RS_Nonsystematic_Matrix(k+m, k, w);
	printf("Non-systematic Vandermonde Matrix:\n\n");
	jm->Print();
	printf("\n");
	delete jm;

	jm = RS_Extended_Matrix(k, m, w);
	printf("Vandermonde Extended Matrix:\n\n");
	jm->Print();
	printf("\n");
	delete jm;

	jm = RS_Classic_Matrix(k, m, w);
	printf("Vandermonde Classic Coding Matrix:\n\n");
	jm->Print();
	printf("\n");
	delete jm;

	return 0;
}
